/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.bean;

import br.cgb.portal.bo.ActivationCodeBO;
import br.cgb.portal.bo.UserBO;
import br.cgb.portal.entity.User;
import br.cgb.portal.entity.enumeration.Sex;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author LUCAS
 */
@ManagedBean
@ViewScoped
public class ActivationBean extends AppBaseBean {

    @EJB
    private UserBO userBO;
    @EJB
    private ActivationCodeBO activationCodeBO;
    private User user;
    private String activationCode;
    private int day;
    private int month;
    private int year;
    private static List<SelectItem> sexList;
    private static List<SelectItem> dayList;
    private static List<SelectItem> monthList;
    private static List<SelectItem> yearList;

    static {
        sexList = new ArrayList<SelectItem>();
        sexList.add(new SelectItem(Sex.FEMALE, "Feminino"));
        sexList.add(new SelectItem(Sex.MALE, "Masculino"));
        dayList = new ArrayList<SelectItem>();
        dayList.add(new SelectItem(0, "..."));
        for (int i = 1; i <= 31; i++) {
            dayList.add(new SelectItem(i, String.valueOf(i)));
        }
        monthList = new ArrayList<SelectItem>();
        monthList.add(new SelectItem(0, "..."));
        for (int i = 1; i <= 12; i++) {
            monthList.add(new SelectItem(i, String.valueOf(i)));
        }
        yearList = new ArrayList<SelectItem>();
        yearList.add(new SelectItem(0, "..."));
        for (int i = 2012; i >= 1900; i--) {
            yearList.add(new SelectItem(i, String.valueOf(i)));
        }
    }

    /**
     * Creates a new instance of ActivationBean
     */
    public ActivationBean() {
    }

    public void confirm() {        
        if (day != 0 && month != 0 && year != 0) {
            Calendar birthdayDate = Calendar.getInstance();
            birthdayDate.set(Calendar.DAY_OF_MONTH, day);
            birthdayDate.set(Calendar.MONTH, month - 1);
            birthdayDate.set(Calendar.YEAR, year);
            user.setBirthdayDate(birthdayDate);
        }
        user.setIncomingDate(Calendar.getInstance());
        user.setCredits(0L);
        user.setValidated(Boolean.TRUE);
        userBO.edit(user);
        redirect("index.xhtml");
    }

    /*
     * Getters and setters
     */
    public User getUser() {
        return (user == null) ? user = activationCodeBO.findUserPerActivationCode(activationCode) : user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getActivationCode() {
        return activationCode;
    }

    public void setActivationCode(String activationCode) {
        this.activationCode = activationCode;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public List<SelectItem> getSexList() {
        return sexList;
    }

    public List<SelectItem> getDayList() {
        return dayList;
    }

    public List<SelectItem> getMonthList() {
        return monthList;
    }

    public List<SelectItem> getYearList() {
        return yearList;
    }
}
